package org.xs.web.http.velocity;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;

public class HeadDirective extends Directive{

	@Override
	public String getName() {
		return "head";
	}

	@Override
	public int getType() {
		return Directive.BLOCK;
	}
	/**
	 * the #head node no body ,so 0 is ? 1 is the body
	 */
	@Override
	public boolean render(InternalContextAdapter context, Writer write, Node node)
			throws IOException, ResourceNotFoundException, ParseErrorException,MethodInvocationException {
		StringWriter sw = new StringWriter(256);
		boolean ok = node.jjtGetChild(0).render(context, sw); // 1 is body
		context.put("head", sw.toString());
		return ok;
	}

}
